﻿/**
 * @author Bill
 */
var thePanelWidth;
Ext.namespace('Ext.ux');
 
/**
 *
 * @class GMapPanel
 * @extends Ext.Panel
 */
Ext.ux.BMapPanel = Ext.extend(Ext.Panel, {
    initComponent: function () {

        var defConfig = {
            plain: true,
            zoomLevel: 14,
            yaw: 180,
            pitch: 0,
            zoom: 0,
            bmapType: 'map',
            border: true
        }
        Ext.applyIf(this, defConfig);
        Ext.ux.BMapPanel.superclass.initComponent.call(this);
    },
    afterRender: function () {

        var wh = this.ownerCt.getSize();
        Ext.applyIf(this, wh);
        Ext.ux.BMapPanel.superclass.afterRender.call(this);
        if (this.bmapType === 'map') {
            this.bmap = new VEMap(this.body.id);
            this.bmap.LoadMap();
        }

        if (typeof this.addControl === 'object' && this.bmapType === 'map') {
            this.bmap.AddControl(this.addControl);
        }

        if (typeof this.setCenter === 'object') {
            if (typeof this.setCenter.geoCodeAddr === 'string') {
                this.geoCodeLookup(this.setCenter.geoCodeAddr);
            } else {
                if (this.bmapType === 'map') {
                    var point = new VELatLng(this.setCenter.Latitude, this.setCenter.Longitude);
                    this.bmap.SetCenterAndZoom(point, this.zoomLevel);
                }
                if (typeof this.setCenter.marker === 'object' && typeof point === 'object') {
                    this.setCenter.marker.SetPoints(point);
                    this.bmap.AddShapeLayer(this.setCenter.marker);

                }
            }
        }

    },
    onResize: function (w, h) {
        thePanelWidth = panelcenter.getWidth();
        if (typeof this.bmap == 'object') {
            this.bmap.Resize(w, h);
            this.bmap.ShowMiniMap(thePanelWidth - 155, 0);
            var resize = document.getElementById('MSVE_minimap_resize');
            resize.style.display = "none";
            var minimap = document.getElementById('MSVE_minimap_transparency');
            if (!document.getElementById('BILL_Resize_New')) {

                var newcon = document.createElement("img");
                newcon.id = 'BILL_Resize_New';
                newcon.style.zIndex = 10000000;
                newcon.src = 'images/hide.gif';
                newcon.style.position = 'absolute';
                newcon.style.top = 'auto';
                newcon.style.left = '139px';
                minimap.appendChild(newcon);
                newcon.onclick = function () {
                    try {
                        if (document.getElementById('BILL_Resize_New').src == 'http://localhost/msextgpsweb/images/hide.gif') {
                            document.getElementById('BILL_Resize_New').src = 'http://localhost/msextgpsweb/images/show.gif';
                            imap.map.ShowMiniMap(thePanelWidth - 12, -142);
                            newcon.style.top = '139px';
                            newcon.style.left = 'auto';
                        }
                        else {
                            document.getElementById('BILL_Resize_New').src = 'http://localhost/msextgpsweb/images/hide.gif';
                            imap.map.ShowMiniMap(thePanelWidth - 155, 0);
                            newcon.style.top = 'auto';
                            newcon.style.left = '139px';
                        }
                    }
                    catch (Error) {
                        alert(thePanelWidth);
                    }
                }


            }

        }

        Ext.ux.BMapPanel.superclass.onResize.call(this, w, h);

    },
    setSize: function (width, height, animate) {
        if (typeof this.bmap == 'object') {
            this.bmap.Resize(width, height);
        }

        Ext.ux.BMapPanel.superclass.setSize.call(this, width, height, animate);
    },
    getMap: function () {

        return this.bmap;

    },
    addMarkers: function (markers) {

        if (Ext.isArray(markers)) {
            for (var i = 0; i < markers.length; i++) {
                var mkr_point = new VELatLng(markers[i].Latitude, markers[i].Longitude);
                markers[i].marker.SetPoints(mkr_point);
                this.AddShapeLayer(markers[i].marker);
            }
        }

    },
    addMarker: function (point, marker, clear, center) {

        Ext.applyIf(marker, G_DEFAULT_ICON);

        if (clear === true) {
            this.bmap.DeleteAllShapes();
        }
        if (center === true) {
            this.bmap.SetCenterAndZoom(point, this.zoomLevel);
        }

        var mark = new VEShapeLayer();
        marker.SetPoints(point);
        mark.AddShape(marker);
        this.bmap.AddShapeLayer(mark);

    },
    geoCodeLookup: function (addr) {
        this.bmap.FindLocations(addr, this.addAddressToMap.createDelegate(this));
    },
    addAddressToMap: function (locations) {
        LatLong = locations[0].LatLong
        point = new VELatLng(LatLong.Longitude, LatLong.Latitude);
        if (typeof this.setCenter.marker === 'object' && typeof point === 'object') {
            this.setCenter.marker.SetPoints(point);
            this.AddShapeLayer(this.setCenter.marker);
        }

    }

});

Ext.reg('bmappanel',Ext.ux.BMapPanel);